package moteur;

/***********************************************************************
 * Module: Impl_Moteur.java Author: 12008505 Purpose: Defines the Class
 * Impl_Moteur
 ***********************************************************************/

import ihm.Editeur;
import Outil.Buffer;
import Outil.PressePapier;
import Outil.Selection;

public class Impl_Moteur implements MoteurEdition {

	private Buffer buffer;

	private PressePapier pp;

	private Selection selection;

	private Editeur ihm;

	public Impl_Moteur(Editeur ihm) {

		pp = new PressePapier();
		buffer = new Buffer();
		this.ihm = ihm;
		selection = new Selection();

	}

	public Buffer getBuffer() {
		return buffer;
	}

	public void setBuffer(Buffer buffer) {
		this.buffer = buffer;
	}

	public Impl_Moteur(Impl_Moteur moteur) {
		pp = moteur.pp;
		buffer = moteur.buffer;
		ihm = moteur.ihm;
		selection = moteur.selection;

	}

	public Impl_Moteur() {

	}

	public void setSelection(int debut, int fin) {
		selection.setDebut(debut);
		selection.setFin(fin);
		System.out.println("position de selection,debut test :"
				+ this.getSelection().getDebut());
		System.out.println("position de selection,fin test :"
				+ this.getSelection().getFin());

	}

	public void setSelection(Selection s) {
		this.selection = s;
	}

	public Selection getSelection() {
		return selection;
	}

	private void effacerSelectionBuffer() {
		int debut = selection.getDebut();
		int fin = selection.getFin();
		StringBuffer zonetravail = buffer.getZoneDeTravail();
		if ((debut - fin) != 0) {
			// buffer.supprimer(debut, fin);
			zonetravail = zonetravail.delete(debut, fin);
		}
	}

	public void hhmettreAJourIHM() {
		ihm.text.setText(this.getBuffer().getZoneDeTravail().toString());

	}

	@Override
	public void couper() {
		int debut = this.selection.getDebut();
		int fin = this.selection.getFin();
		if (debut > fin) {
			int intermediaire = debut;
			debut = fin;
			fin = intermediaire;
		}

		String couper = this.buffer.getSelectedText(debut, fin);

		if (couper != null) {

			if (buffer.getZoneDeTravail().length() < fin) {
				int bias = fin - buffer.getZoneDeTravail().length();
				fin = buffer.getZoneDeTravail().length();
				debut = debut - bias;
			}

			buffer.supprimer(debut, fin);
			pp.setContenu(couper);

			// selection.setFin(selection.getDebut());
		}
		// mettreAJourIHM();
		ihm.text.cut();
		// ihm.text.setCaretPosition(debut);

		System.out.println("le buffer :" + this.buffer.getZoneDeTravail());
		System.out.println("le pressepapier :" + this.pp.getContenu());
		System.out
				.println("le debut de selection:" + this.selection.getDebut());
		System.out.println("le fin de selection:" + this.selection.getFin());

	}

	@Override
	public void copier() {
		int debut = this.selection.getDebut();
		int fin = this.selection.getFin();
		String copie = this.buffer.getSelectedText(debut, fin);

		if (copie != null) {
			pp.setContenu(copie);
		}

		System.out.println("le buffer :" + this.buffer.getZoneDeTravail());
		System.out.println("le pressepapier :" + this.pp.getContenu());
		System.out
				.println("le debut de selection:" + this.selection.getDebut());
		System.out.println("le fin de selection:" + this.selection.getFin());

	}

	public PressePapier getPp() {
		return pp;
	}

	public void setPp(PressePapier pp) {
		this.pp = pp;
	}

	@Override
	public void coller() {
		supprimerbis();
		ihm.text.cut();

		this.getBuffer().ajouterZoneDeTravailString(pp.getContenu(),
				this.getSelection().getDebut());

		ihm.text.insert(pp.getContenu(), this.getSelection().getDebut());

		System.out.println("le buffer :" + this.buffer.getZoneDeTravail());
		System.out.println("le pressepapier :" + this.pp.getContenu());
		System.out
				.println("le debut de selection:" + this.selection.getDebut());
		System.out.println("le fin de selection:" + this.selection.getFin());

	}

	public void insererText(char c) {
		int debut = selection.getDebut();
		System.out.println("le debut de selection--en avant:" + debut);
		// effacerSelectionBuffer();
		buffer.ajouterZoneDeTravailChar(c, debut);

		System.out.println("le buffer :" + this.buffer.getZoneDeTravail());
		System.out.println("le pressepapier :" + this.pp.getContenu());
		System.out
				.println("le debut de selection:" + this.selection.getDebut());
		System.out.println("le fin de selection:" + this.selection.getFin());

	}

	public void supprimer() {

		int debut = this.getSelection().getDebut();

		int fin = this.getSelection().getFin();
		if (debut > fin) {
			int intermediaire = debut;
			debut = fin;
			fin = intermediaire;
		}

		if (debut == fin) {

			if (debut != 0) {

				this.getBuffer().supprimer(debut - 1, debut - 1);
				// this.mettreAJourIHM();

				ihm.text.setCaretPosition(debut - 1);
				this.setSelection(debut - 1, debut - 1);

				// System.out.println("le buffer d= f :"
				// + this.getBuffer().getZoneDeTravail());

			}
		} else {

			this.getBuffer().supprimer(debut, fin);

			this.setSelection(debut - 1, debut - 1);

			ihm.text.cut();

			// System.out.println("position de selection ihm FIN :"
			// + this.getSelection().getFin());
			//
			// System.out.println("le buffer :"
			// + this.getBuffer().getZoneDeTravail());
		}

		System.out.println("le buffer :" + this.buffer.getZoneDeTravail());
		System.out.println("le pressepapier :" + this.pp.getContenu());
		System.out
				.println("le debut de selection:" + this.selection.getDebut());
		System.out.println("le fin de selection:" + this.selection.getFin());

	}

	public void supprimerbis() {
		if (selection.getLength() != 0) {
			effacerSelectionBuffer();
		}

		// else {
		// if (selection.getDebut() != 0) {
		// buffer.supprimer(selection.getDebut() - 1, selection.getDebut());
		// }
		// }

	}

}